//
//  TXPRadioPageView.m
//  StayAwhile
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TXPRadioPageView.h"
#import <Masonry.h>
#import "TXP_RadioCollectionViewCell.h"
#import <UIImageView+WebCache.h>

@interface TXPRadioPageView ()<UICollectionViewDataSource,UICollectionViewDelegate,TXP_RadioCollectionViewCellDelegate>

@property (nonatomic,strong)UICollectionViewFlowLayout *myFlowLayout;
@property (nonatomic,strong)UICollectionView *myCollectionView;
@end
@implementation TXPRadioPageView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame]) {
    
        [self craeteSubView];
    }

    return self;
}
- (void)setAllImgArray:(NSMutableArray *)allImgArray{
    
    _allImgArray = allImgArray;
     [self.myCollectionView reloadData];
}
- (void)craeteSubView{
    
    _allImgArray = [NSMutableArray array];
    self.myFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.myFlowLayout.itemSize = self.bounds.size;
    self.myFlowLayout.minimumInteritemSpacing = 0;
    self.myFlowLayout.minimumLineSpacing = 0;
   // self.myFlowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.myFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 180) collectionViewLayout:self.myFlowLayout];
    self.myCollectionView.pagingEnabled = YES;
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myCollectionView.showsHorizontalScrollIndicator = NO;
    [self.myCollectionView registerClass:[TXP_RadioCollectionViewCell class] forCellWithReuseIdentifier:@"RadioImg"];
    [self addSubview:self.myCollectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.allImgArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    TXP_RadioCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RadioImg" forIndexPath:indexPath];
    cell.delegate = self;
    cell.tag = 1000 + indexPath.row;
    if ([_allImgArray[indexPath.item] isKindOfClass:[NSString class]]) {
        if ([_allImgArray[indexPath.item] hasPrefix:@"http"]) {
    
            [cell.radioImgView sd_setImageWithURL:_allImgArray[indexPath.item] placeholderImage:nil];
        }else if ([_allImgArray[indexPath.item] hasPrefix:@"/"]||[_allImgArray[indexPath.item] hasPrefix:@"file"]){
            cell.radioImgView.image = [UIImage imageWithContentsOfFile:_allImgArray[indexPath.item]];
            
        }else{
            cell.radioImgView.image = [UIImage imageNamed:_allImgArray[indexPath.row]];
        }
        
    }else if ([_allImgArray[indexPath.item] isKindOfClass:[NSURL class]]){
        
        [cell.radioImgView sd_setImageWithURL:_allImgArray[indexPath.item] placeholderImage:nil];
        
    }else if ([_allImgArray[indexPath.item] isKindOfClass:[UIImage class]]){
        cell.radioImgView.image = self.allImgArray[indexPath.item];
    }
    return cell;
}
- (void)TXPImgDetailCellDidSelectedWithIndex:(NSInteger)cellIndex{
    
    
        [self.myDelegate TXPImgView:self didSelectIndex:cellIndex];
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat content = scrollView.contentOffset.x;
    if (content >= 0 && content <= scrollView.contentSize.width - [UIScreen mainScreen].bounds.size.width) {
        NSIndexPath *contenIndexPath = [NSIndexPath indexPathForItem:scrollView.contentOffset.x/[UIScreen mainScreen].bounds.size.width  inSection:0];
        NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:scrollView.contentOffset.x/[UIScreen mainScreen].bounds.size.width +1 inSection:0];
        TXP_RadioCollectionViewCell *contenCell = (TXP_RadioCollectionViewCell *)[self.myCollectionView cellForItemAtIndexPath:contenIndexPath];
        TXP_RadioCollectionViewCell *nextCell = (TXP_RadioCollectionViewCell *)[self.myCollectionView cellForItemAtIndexPath:nextIndexPath];
        CGRect contentImageViewRect = contenCell.radioImgView.frame;
        CGRect nextImageViewRect = nextCell.radioImgView.frame;
        contentImageViewRect.origin.x = 0 + (content - contenIndexPath.item * [UIScreen mainScreen].bounds.size.width) / 3;
        nextImageViewRect.origin.x = 0 - [UIScreen mainScreen].bounds.size.width / 3 + (content - contenIndexPath.item * [UIScreen mainScreen].bounds.size.width) / 3;

        contenCell.radioImgView.frame = contentImageViewRect;
        nextCell.radioImgView.frame = nextImageViewRect;
    }
}
@end
